package com.simtop.service.impl;

import com.baidu.aip.contentcensor.AipContentCensor;
import com.baidu.aip.contentcensor.EImgType;
import com.simtop.pojo.resource.Resources;
import com.simtop.service.CheckContent;
import com.simtop.service.ResourcesService;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;


/**
 * @author lvyongwei
 */
@Service
public class CheckContentImpl implements CheckContent {

    @Autowired
    private AipContentCensor aipContentCensor;

    @Autowired
    private ResourcesService resourcesService;

    private Logger logger = LoggerFactory.getLogger(CheckContentImpl.class);

    /**
     * 审核图片
     * @param path
     * @return
     */
    @Override
    @Async
    public void checkPhoto(String path,Long id) {
        try{
            JSONObject res = aipContentCensor.imageCensorUserDefined(path, EImgType.FILE, null);
            Resources resources = new Resources();
            resources.setId(id);
            resources.setCheckResult(res.toString());
            boolean b = resourcesService.updateResources(resources);
            if(!b){
                logger.error("图片审核后插入数据库失败,时间是{},资源id为{}",new Date(),id);
            }
        }catch (Exception e){
            logger.error("文件审核插入失败，请检查");
        }
    }

    /**
     * 文本审核
     * @param text
     * @return
     */
    @Override
    public String checkText(String text) {
        try{
            // 参数为本地图片路径
            JSONObject response = aipContentCensor.textCensorUserDefined(text);
            return response.toString();
        }catch (Exception e){
            logger.error("文本审核失败，请检查");
            return null;
        }
    }

    /**
     * 音频审核
     * @param voice
     * @return
     */
    @Async
    @Override
    public void checkVoice(String voice,Long id) {
        try{
            //可选参数配置
            HashMap<String, String> options=new HashMap(2);
            options.put("rawText","false");
            options.put("split","true");
            // 参数为本地语音
            String suffix = voice.substring(voice.lastIndexOf(".")+1,voice.length());
            //审核音频
            JSONObject response = aipContentCensor.voiceCensorUserDefined(voice,EImgType.FILE,suffix,options);
            //添加审核结果
            Resources resources = new Resources();
            resources.setId(id);
            resources.setCheckResult(response.toString());
            boolean b = resourcesService.updateResources(resources);
            if(!b){
                logger.error("音频审核后插入数据库失败,时间是{},资源id为{}",new Date(),id);
            }
        }catch (Exception e){
            logger.error("音频审核插入失败，请检查");
        }
    }


    /**
     * 视频审核
     * @param video
     * @return
     */
    @Async
    @Override
    public void checkVideo(String video, Long id, HttpServletRequest request) {
        try{
            String requestURI = request.getHeader("host");
            String contextPath = request.getContextPath();
            //视频url 地址
            String videoUrl = requestURI + contextPath + video;
            //视频名字
            String name="videoName";
            //视频id;
            String extId="id";
            //设置链接超时时间
            JSONObject res=aipContentCensor.videoCensorUserDefined(name, videoUrl, extId, null);

            //添加审核结果
            Resources resources = new Resources();
            resources.setId(id);
            resources.setCheckResult(res.toString());
            boolean b = resourcesService.updateResources(resources);
            if(!b){
                logger.error("视频审核后插入数据库失败,时间是{},资源id为{}",new Date(),id);
            }
        }catch (Exception e){
            logger.error("视频审核插入失败，请检查");
        }

    }
}
